package com.zhu.exercise.array;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class TwoSum {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],i);
        }

        for (int i = 0; i < nums.length; i++) {
            int nums2 = target-nums[i];
            if(map.containsKey(nums2)){
                if(map.get(nums2)!=i){
                    result[0]=i;
                    result[1]=map.get(nums2);
                    break;
                }
            }
        }
        return result;
    }

    @Test
    public void run(){
        int[] nums = {3,2,4};
        int target = 6;
        int [] result = twoSum(nums,target);
        for (int i : result) {
            System.out.println(i);
        }
    }
}
